package com.crm.system.service.impl.orderManager;

import com.crm.common.model.DhPageInfo;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.customerManager.CustomerProductDetailExchangeCode;
import com.crm.model.qo.orderManager.OrderRawDataQO;
import com.crm.model.vo.orderManager.CustomerProductOrderVO;
import com.crm.model.vo.orderManager.OrderRawDataVO;
import com.crm.service.customerManager.CustomerDbService;
import com.crm.service.orderManager.CustomerProductOrderDbService;
import com.crm.service.orderManager.OrderRawDataDbService;
import com.crm.system.service.orderManager.OrderRawDataService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ Author ：pym
 * @ Date   ：2021/7/23 10:08
 */
@Service
public class OrderRawDataServiceImpl implements OrderRawDataService {

    @Autowired
    private OrderRawDataDbService orderRawDataDbService;

    @Autowired
    private CustomerProductOrderDbService customerProductOrderDbService;

    @Autowired
    private CustomerDbService customerDbService;

    /**
     * 订单原始数据
     * @Author pym
     * @Date  2021/7/23 11:08
     * @param pageIndex -页码
     * @param pageSize -分页大小
     * @param sortName -排序列名字
     * @param sortOrder -排序规则(asc/desc)
     * @param orderRawDataQO -查询条件
     * @return {@link {com.crm.common.model.DhPageInfo<com.crm.model.vo.orderManager.OrderRawDataVO>}}
     */
    @Override
    public DhPageInfo<OrderRawDataVO> selectOrderRawDataList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, OrderRawDataQO orderRawDataQO) {
        List<OrderRawDataVO> orderRawDataList = new ArrayList<OrderRawDataVO>();
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        orderRawDataList = orderRawDataDbService.selectOrderRawDataList(orderRawDataQO);
        PageInfo<OrderRawDataVO> pageInfo = new PageInfo<OrderRawDataVO>(orderRawDataList);
        return new DhPageInfo<OrderRawDataVO>(pageInfo, orderRawDataList);
    }

    /**
     * 发兑换码
     * @return
     */
    @Override
    public Integer sendExchangeCode() {
        Date date = new Date();
        Integer num = 0;
        OrderRawDataQO orderRawDataQO = new OrderRawDataQO();
        List<OrderRawDataVO> list = orderRawDataDbService.selectOrderRawDataList(orderRawDataQO);
        if(list != null && list.size() > 0){
            for(OrderRawDataVO vo : list) {
                if(!StringUtils.isBlank(vo.getOrderNumber())) {
                    CustomerProductOrderVO order = customerProductOrderDbService.getOrderInfoByNumber(vo.getOrderNumber());
                    if(order != null) {
                        if(!StringUtils.isBlank(vo.getExchangeCode())) {
                            CustomerProductDetailExchangeCode ex = new CustomerProductDetailExchangeCode();
                            ex.setExchangeCodeId(CommonUtils.getGUID());
                            ex.setExchangeCodeCrmId(vo.getExchangeCode());
                            ex.setCreatePeopleId("71902468-8dee-4e30-b554-456d2140c7d9");
                            ex.setCreateTm(date);
                            ex.setUpdatePeopleId("71902468-8dee-4e30-b554-456d2140c7d9");
                            ex.setUpdateTm(date);
                            ex.setProductOrderId(order.getProductOrderId());
                            customerDbService.addCustomerProductDetailExchangeCode(ex);
                            num+=1;
                        }

                    }
                }
            }
        }
        return num;
    }
}
